package com.example.todo.Activities;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.example.todo.R;
import com.example.todo.DAL.IItemRepository;
import com.example.todo.DAL.ItemRepository;
import com.example.todo.Entities.Item;

public class EditItemActivity extends Activity {

	private EditText _txtName;
	private Spinner _spPriority;
	private Item _item;
	private IItemRepository _itemRepository;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_item);

		_item = (Item) getIntent().getExtras().getSerializable("item");
		_itemRepository = new ItemRepository(getApplicationContext());
		_txtName = (EditText) findViewById(R.id.txtName);
		_spPriority = (Spinner) findViewById(R.id.spPriority);

		InitEvenHandlers();
		LoadControls();

	}

	private void LoadControls() {
		_txtName.setText(_item.get_name());

		_spPriority.setSelection(_item.get_priority());
	}

	private void InitEvenHandlers() {
		final Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
		btnSubmit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				_item.set_name(_txtName.getText().toString());
				_item.set_priority(_spPriority.getSelectedItemPosition());
				if (_item.get_id() == 0) {
					_itemRepository.Add(_item);
				} else {
					_itemRepository.Update(_item);
				}
				setResult(RESULT_OK);
				finish();
			}
		});

		final Button btnMarkFinished = (Button) findViewById(R.id.btnMarkFinished);
		if (_item.get_id() == 0) {
			btnMarkFinished.setVisibility(View.GONE);
		} else {
			btnMarkFinished.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					_item.set_finished(true);
					_itemRepository.Update(_item);
					setResult(RESULT_OK);
					finish();
				}
			});
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.edit_item, menu);
		return true;
	}

}
